<template>
	<view class="auth-modal">
		<text class="auth-text">登录会员 获取工作任务 尊享会员权益</text>
		<button class="auth-btn" @click="handleLogin">一键登录/注册</button>
	</view>
</template>

<script>
	export default {
		name: "auth-modal",
		data() {
			return {
				code: '',
			};
		},
		methods: {
			handleLogin() {
				getApp().showLoading();
				uni.login({
					provider: "weixin",
					success(loginRes) {
						console.log(loginRes);
						let code = loginRes.code;
						uni.request({
							url: "https://bydapi.vaubang.cn/api/v1/WxOpen/Login?code=" +
								code,
							method: "post",
							header: {
								"content-type": "application/json;charset=utf-8;"
							},
							success(res) {
								uni.hideLoading();
								console.log('登陆信息', res);
								// 当登录者已经绑定了openid时，直接保存登录信息返回的token以及公司信息
								if (res.data.code == 200) {
									uni.setStorageSync("isLogin", true);
									uni.setStorageSync('loginInfo', res.data
										.data);
									getApp().globalData.loginInfo = res.data
										.data;
								} else if (res.data.code == 210) {
									uni.setStorageSync("userData", res.data.data);
									uni.navigateTo({
										url: '/packageA/bind/bind'
									})
								}
							},
							fail(err) {
								console.log(err);
							}
						})
					}
				})
			},
		},
	}
</script>

<style lang="less">
	.auth-modal {
		display: flex;
		flex-direction: column;
		align-items: center;
		margin-top: 50px;

		.auth-text {
			font-size: 14px;
			color: #666;
		}

		.auth-btn {
			width: 150px;
			height: 36px;
			line-height: 36px;
			font-size: 14px;
			color: #008aff;
			border: 1px solid #008aff;
			margin-top: 15px;
		}
	}
</style>
